package org.jeecg.modules.tls.csmg.csquestionsandanswers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.modules.tls.csmg.csanswerlike.entity.CsAnswerLike;
import org.jeecg.modules.tls.csmg.csanswerlike.mapper.CsAnswerLikeMapper;
import org.jeecg.modules.tls.csmg.csquestionsandanswers.entity.CsQuestionsandanswers;
import org.jeecg.modules.tls.csmg.csquestionsandanswers.mapper.CsQuestionsandanswersMapper;
import org.jeecg.modules.tls.csmg.csquestionsandanswers.service.ICsQuestionsandanswersService;
import org.jeecg.modules.tls.csmg.viewmodel.qa.AnswerListVM;
import org.jeecg.modules.tls.csmg.viewmodel.qa.AnswerVM;
import org.jeecg.modules.tls.csmg.viewmodel.qa.QuestionListVM;
import org.jeecg.modules.user.entity.SysUserDemo;
import org.jeecg.modules.user.mapper.SysUserDemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: cs_questionsandanswers
 * @Author: jeecg-boot
 * @Date:   2022-08-05
 * @Version: V1.0
 */
@Service
public class CsQuestionsandanswersServiceImpl extends ServiceImpl<CsQuestionsandanswersMapper, CsQuestionsandanswers> implements ICsQuestionsandanswersService {

    @Autowired
    CsQuestionsandanswersMapper csQuestionsandanswersMapper;
    @Autowired
    SysUserDemoMapper sysUserDemoMapper;
    @Autowired
    CsAnswerLikeMapper csAnswerLikeMapper;

    @Override
    public Boolean addQA(String userId, String courseId, String pid, String content) {
        CsQuestionsandanswers csQuestionsandanswers = new CsQuestionsandanswers();
        csQuestionsandanswers.setContent(content);
        csQuestionsandanswers.setUserId(userId);
        csQuestionsandanswers.setCourseId(courseId);
        csQuestionsandanswers.setPid(pid);

        return csQuestionsandanswersMapper.insert(csQuestionsandanswers)>0;
    }

    @Override
    public Boolean deleteQA(String id) {
        CsQuestionsandanswers csQuestionsandanswers = csQuestionsandanswersMapper.selectById(id);
        if(csQuestionsandanswers.getPid().equals("0")){
            QueryWrapper<CsQuestionsandanswers> csQuestionsandanswersQueryWrapper = new QueryWrapper<>();
            csQuestionsandanswersQueryWrapper.eq("pid",id);
            csQuestionsandanswersMapper.selectList(csQuestionsandanswersQueryWrapper).stream().forEach(i->{
                csQuestionsandanswersMapper.deleteById(i.getId());
            });
        }
            return csQuestionsandanswersMapper.deleteById(id)>0;
    }

    @Override
    public IPage<QuestionListVM> getQuestionPage(String courseId, Integer pageIndex, Integer pageSize) {
        //查询条件，将对应课程的问题按照时间降序查出来
        QueryWrapper<CsQuestionsandanswers> questionsandanswersQueryWrapper = new QueryWrapper<>();
        questionsandanswersQueryWrapper.eq("pid","0").eq("course_id",courseId).orderByDesc("create_time");
        //分页插件
        Page<CsQuestionsandanswers> page = new Page<>(pageIndex,pageSize);
        IPage<CsQuestionsandanswers> questionsandanswersIPage = csQuestionsandanswersMapper.selectPage(page,questionsandanswersQueryWrapper);

        List<QuestionListVM> questionListVMList = questionsandanswersIPage.getRecords().stream().map(i->{
            QuestionListVM questionListVM = new QuestionListVM();

            //获取回复数量
            QueryWrapper<CsQuestionsandanswers> csQuestionsandanswersQueryWrapper = new QueryWrapper<>();
            csQuestionsandanswersQueryWrapper.eq("pid",i.getId());
            Integer replyNum = Math.toIntExact(csQuestionsandanswersMapper.selectCount(csQuestionsandanswersQueryWrapper));

            //获取用户信息
            SysUserDemo sysUserDemo = sysUserDemoMapper.selectById(i.getUserId());

            //设置问题列表模型
            questionListVM.setId(i.getId());
            questionListVM.setContent(i.getContent());
            questionListVM.setCreateTime(i.getCreateTime());
            questionListVM.setAvatar(sysUserDemo.getAvatar());
            questionListVM.setUserName(sysUserDemo.getRealname());
            questionListVM.setUserId(sysUserDemo.getId());
            questionListVM.setReplyNum(replyNum);

            return questionListVM;
        }).collect(Collectors.toList());

        IPage<QuestionListVM> questionListVMIPage = new Page<>();
        questionListVMIPage.setPages(questionsandanswersIPage.getPages());
        questionListVMIPage.setCurrent(questionsandanswersIPage.getCurrent());
        questionListVMIPage.setSize(questionsandanswersIPage.getSize());
        questionListVMIPage.setTotal(questionsandanswersIPage.getTotal());
        questionListVMIPage.setRecords(questionListVMList);

        return questionListVMIPage;
    }

    @Override
    public Boolean setSatisfied(String pid, String id) {
        CsQuestionsandanswers csQuestionsandanswers = csQuestionsandanswersMapper.selectById(pid);
        csQuestionsandanswers.setSatisfiedId(id);
        return csQuestionsandanswersMapper.updateById(csQuestionsandanswers)>0;
    }

    @Override
    public Boolean cancelSatisfied(String pid) {

        return csQuestionsandanswersMapper.setSatisfiedIdById(pid)>0;
    }

    @Override
    public AnswerListVM getAnswerList(String id, String userId) {
        AnswerListVM answerListVM = new AnswerListVM();

        CsQuestionsandanswers question = csQuestionsandanswersMapper.selectById(id);
        if(question.getSatisfiedId()!=null&&!question.getSatisfiedId().equals("null")){
            AnswerVM  satisfiedAnswerVM = new AnswerVM();
            //获取满意回答问答数据
            CsQuestionsandanswers satisfiedAnswer = csQuestionsandanswersMapper.selectById(question.getSatisfiedId());
            //获取满意回答用户信息
            SysUserDemo satisfiedAnswerSysUserDemo = sysUserDemoMapper.selectById(satisfiedAnswer.getUserId());
            //获取满意回答点赞数据
            QueryWrapper<CsAnswerLike> satisfiedAnswerLikeQueryWrapper = new QueryWrapper<>();
            satisfiedAnswerLikeQueryWrapper.eq("qa_id",satisfiedAnswer.getId());
            Integer satisfiedAnswerReplyLikeNum = Math.toIntExact(csAnswerLikeMapper.selectCount(satisfiedAnswerLikeQueryWrapper));
            satisfiedAnswerLikeQueryWrapper.eq("user_id",userId);
            satisfiedAnswerVM.setWhetherLike(csAnswerLikeMapper.selectOne(satisfiedAnswerLikeQueryWrapper)==null?false:true);
            //设置满意回答内容
            satisfiedAnswerVM.setReplyLikeNum(satisfiedAnswerReplyLikeNum);
            satisfiedAnswerVM.setContent(satisfiedAnswer.getContent());
            satisfiedAnswerVM.setId(satisfiedAnswer.getId());
            satisfiedAnswerVM.setCreateTime(satisfiedAnswer.getCreateTime());
            satisfiedAnswerVM.setUserId(satisfiedAnswerSysUserDemo.getId());
            satisfiedAnswerVM.setUserName(satisfiedAnswerSysUserDemo.getRealname());
            satisfiedAnswerVM.setAvatar(satisfiedAnswerSysUserDemo.getAvatar());

            answerListVM.setSatisfiedAnswer(satisfiedAnswerVM);
        }

        //获取其他回答数据
        QueryWrapper<CsQuestionsandanswers> questionsandanswersQueryWrapper = new QueryWrapper<>();
        questionsandanswersQueryWrapper.eq("pid",id);
        if(question.getSatisfiedId()!=null){
            questionsandanswersQueryWrapper.ne("id",question.getSatisfiedId());
        }
        questionsandanswersQueryWrapper.orderByDesc("create_time");
        List<CsQuestionsandanswers> csQuestionsandanswersList = csQuestionsandanswersMapper.selectList(questionsandanswersQueryWrapper);
        List<AnswerVM> answerVMList = csQuestionsandanswersList.stream().map(i->{
            AnswerVM answerVM = new AnswerVM();
            //获取该回复的用户信息
            SysUserDemo sysUserDemo = sysUserDemoMapper.selectById(i.getUserId());
            //获取该回复的点赞数据
            QueryWrapper<CsAnswerLike> answerLikeQueryWrapper = new QueryWrapper<>();
            answerLikeQueryWrapper.eq("qa_id",i.getId());
            Integer answerReplyLikeNum = Math.toIntExact(csAnswerLikeMapper.selectCount(answerLikeQueryWrapper));
            answerLikeQueryWrapper.eq("user_id",userId);
            answerVM.setWhetherLike(csAnswerLikeMapper.selectOne(answerLikeQueryWrapper)==null?false:true);
            //设置回答内容
            answerVM.setReplyLikeNum(answerReplyLikeNum);
            answerVM.setId(i.getId());
            answerVM.setContent(i.getContent());
            answerVM.setCreateTime(i.getCreateTime());
            answerVM.setUserId(sysUserDemo.getId());
            answerVM.setAvatar(sysUserDemo.getAvatar());
            answerVM.setUserName(sysUserDemo.getRealname());
            return answerVM;
        }).collect(Collectors.toList());
        answerListVM.setAnswerVMList(answerVMList);

        return answerListVM;
    }
}
